let sql = require(`${globalThis.baseDirname}/sql.js`)
let { v4: uuid } = require('uuid')
let dayjs = require('dayjs')
function run(app) {
    app.post('/update/commonword/words', async (req, res) => {
        let sqlString = `update CommonWord set words='${JSON.stringify(req.body)}' where id = '1'`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/select/commonword/words', async (req, res) => {
        let results = await sql(`select * from CommonWord`)
        res.send(results)
    })
    app.post('/insert/commonsentence', async (req, res) => {
        let {enQuestion, zhQuestion, enAsk, zhAsk} = req.body
        let id = uuid()
        let datetime = dayjs().format('YYYY-MM-DD HH:mm:ss')
        let sqlString = `insert into CommonSentence (id, enQuestion, zhQuestion, enAsk, zhAsk, datetime) values ('${id}', '${enQuestion}', '${zhQuestion}', '${enAsk}', '${zhAsk}', '${datetime}')`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/select/commonsentence', async (req, res) => {
        let sqlString =`select * from CommonSentence order by datetime desc`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/delete/commonsentence', async (req, res) => {
        let id = req.body.id
        let sqlString = `delete from CommonSentence where id = '${id}'`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/update/commonsentence', async (req, res) => {
        let {id, enQuestion, zhQuestion, enAsk, zhAsk} = req.body
        let sqlString = `update CommonSentence set enQuestion='${enQuestion}', zhQuestion='${zhQuestion}', 
                  enAsk='${enAsk}', zhAsk='${zhAsk}' where id = '${id}'`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/update/speak', async (req, res) => {
        let sqlString = `update Speak set list='${JSON.stringify(req.body)}' where id = '1'`
        let results = await sql(sqlString)
        res.send(results)
    })
    app.post('/select/speak', async (req, res) => {
        let sqlString = `select * from Speak where id = '1'`
        let results = await sql(sqlString)
        res.send(results)
    })
}

module.exports = run